home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: Encapsulation question.
- Date: 5 Mar 1996 22:24:08 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4hieu8$hfh@news1.usa.pipeline.com>
- References: <4hg701$goi@insosf1.netins.net>
- NNTP-Posting-Host: pipe16.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 05, 1996 00:59:35 in article <Encapsulation question.>,
- 'hhowe@trgnet.com (Harold Howe)' wrote:
-
-
- >Greetings.
- >
- >Does the following violate encapsulation? Should an object tell a private
-
- >member the address of other private members? I am using this on a grander
-
- >scale, in which the Application class tells a private dialog object the
- >address of a private configuration structure? The dialog class needs
- access
- >to the structure so it can modify it, and this is how I tell the dialog
- where
- >to look. Aside from using globals, does anyone have any better
- suggestions?
- >
- >class Application
- >{
- >private:
- >int j;
- >Dialog *dlg;
- >public:
- >Application(void) { dlg = new Dialog(&j); }
- >}
- >
- >class Dialog
- >{
- >private:
- >int *ptr;
- >... // plus other members which access ptr
- >public:
- >Dialog(int *addr) { ptr = addr } ;
- >}
- >
- >int main()
- >{
- >Application app;
- >}
- >
-
- Pass a reference to Application to Dialog consructor instead. Then use
- the object to access the members. In order to do this, you must one of:
-
- * make appropriate members of public
- * declare Application a friend of Dialog
- * Provide public Get/Set accessors.
-
-
- class Application;
-
- class Dialog
- {
- private:
- Application & app;
- public:
- Dialog(Application& app) : app(app) {}
- void somefunc() { dosomething(app.someslot); }
- };
-
- class Application
- {
- private:
- int j;
- Dialog *dlg;
- public:
- Application() { dlg = new Dialog(*this); }
- };
-
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-